package com.zklh.leetcode.top100;

import java.util.Arrays;

/**
 * 颜色分类算法实现
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/12/18
 */
public class Solution75 {
    public void sortColors(int[] nums) {
        int left = 0;
        int right = nums.length-1;

        int current = 0;
        while (current <= right) {
            if (nums[current] == 0) {
                if (current == left) {
                    current++;
                    left++;
                } else {
                    swap(nums, current, left++);
                }
            } else if (nums[current] == 2) {
                swap(nums, current, right--);
            } else if (nums[current] == 1) {
                current++;
            }
        }
    }

    private void swap(int[] nums, int i1, int i2) {
        int temp = nums[i1];
        nums[i1] = nums[i2];
        nums[i2] = temp;
    }

    public static void main(String[] args) {
        Solution75 solution = new Solution75();
        int[] nums = {2,0,2,1,1,0};
        solution.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}